package org.agilelang.fgl.scanner;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Scanner {

    int line = 1;
    int linet = 1;
    String error;
    HashMap<String, String> sentences = new HashMap<String, String>();

    public Scanner() {
    }

    public boolean process(String pathFile) {
        int line = 1;
        String file = getStringFile(pathFile);
        if (file == null) {
            this.error = "No se puede abrir el fichero";
            return false;
        }

        String ls = System.getProperty("line.separator");
        int len_file = file.length();

        StringBuilder strContent = new StringBuilder("");
        int current_state = 1;

        TransitionMatrix transition_matrix = new TransitionMatrix();
        Token token_status = new Token();

        for (int i = 0; i < len_file; i++) {
            char ch = file.charAt(i);
            current_state = transition_matrix.move(current_state, ch);
            if (current_state == 0) {
                this.error = pathFile + " Error line :" + line;
                return false;
            }
            if (ch != 13 && ch != 10) {
                strContent.append(ch);
            } else if (ch == 13) {
                line++;
            }
            if (token_status.stateEnd(current_state)) {
                current_state = 1;
                sentences.put("Line :" + String.valueOf(line) + " File :" + pathFile, strContent.toString().trim());
                strContent = new StringBuilder("");
            }
        }
        return true;
    }

    public String getStringFile(String pathFile) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(pathFile));
            String _line = "";
            StringBuilder stringBuilder = new StringBuilder();
            String ls = System.getProperty("line.separator");
            while ((_line = reader.readLine()) != null) {
                stringBuilder.append(_line);
                stringBuilder.append(ls);
                linet++;
            }
            String file = stringBuilder.toString();
            file = file.replaceAll("//(.*)", "");
            return file;
        } catch (IOException ex) {
            Logger.getLogger(Scanner.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                reader.close();
            } catch (IOException ex) {
                Logger.getLogger(Scanner.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return null;
    }

    public String getError() {
        return error;
    }

    public HashMap getLines() {
        return this.sentences;
    }
}
